package com.wxyzz.cem.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.wxyzz.cem.pojo.PaymentProject;
import com.wxyzz.cem.mapper.PaymentProjectMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wxyzz.cem.pojo.enums.PaymentProjectIsDelete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * <p>
 * 缴费项目 服务实现类
 * </p>
 *
 * @author xc
 * @since 2024-07-04
 */
@Service
public class PaymentProjectService extends ServiceImpl<PaymentProjectMapper, PaymentProject> implements IService<PaymentProject> {
    @Autowired
    private PaymentProjectMapper paymentProjectMapper;
    @Autowired
    private PaymentRecordService paymentRecordService;

    public Page<PaymentProject> getAllProjectByPage(Integer page,Integer size,String text, Integer classId,Integer userId) {
        LambdaQueryWrapper<PaymentProject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(text),PaymentProject::getTitle,text)
                .or().like(StrUtil.isNotBlank(text),PaymentProject::getDescription,text);
        queryWrapper.eq(PaymentProject::getClassId,classId);
        Page<PaymentProject> p = new Page<>(page,size);
        Page<PaymentProject> paymentProjectPage = paymentProjectMapper.selectPage(p,queryWrapper);
        List<PaymentProject> paymentProjects = paymentProjectPage.getRecords();
        for(PaymentProject paymentProject : paymentProjects) {
            paymentProject.setIsPaid(paymentRecordService.isPaid(paymentProject.getId(), userId));
        }
        paymentProjectPage.setRecords(paymentProjects);
        return paymentProjectPage;
    }

    public boolean deleteProject(Integer paymentProjectId) {
        PaymentProject paymentProject = this.getById(paymentProjectId);
        paymentProject.setIsDelete(PaymentProjectIsDelete.YES);
        return this.updateById(paymentProject);
    }
}
